/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo.initcolumnsstragety;

import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.DataTypeInterfaceGeneratorContext;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo.InitColumnsGenerator;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.MethodInvocation;

public abstract class BaseAddColumnStragety {

  protected final Block block;
  protected IGspCommonDataType commonDataType;
  protected final IGspCommonField field;
  protected final AST ast;
  protected DataTypeInterfaceGeneratorContext parentContext;
  protected ExtendAddPropertyInfoProcessor extendAddPropertyInfoProcessor;

  public BaseAddColumnStragety(Block block, IGspCommonDataType commonDataType, IGspCommonField field,AST ast,
      DataTypeInterfaceGeneratorContext parentContext,ExtendAddPropertyInfoProcessor extendAddPropertyInfoProcessor)
{

  this.block = block;
  this.commonDataType = commonDataType;
  this.field = field;
  this.ast = ast;
  this.parentContext = parentContext;

  this.extendAddPropertyInfoProcessor = extendAddPropertyInfoProcessor;
}


  public void buildAddPropertyInfoStatement() {
    beforeAddMethodInvocation();
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setName(ast.newSimpleName(getMethodName()));
    addArguments(methodInvocation);
    block.statements().add(ast.newExpressionStatement(methodInvocation));
  }

  protected void beforeAddMethodInvocation() {

  }

  public void addArguments(MethodInvocation methodInvocation)
  {
    addPropertyNameStatement(methodInvocation,field.getLabelID());
    if(useSimpleAddStatment()) {
      addSimpleAddStatement(methodInvocation);
      if(addExtendPropertyArgs())
    extendAddPropertyInfoProcessor.addSimpleExtendPropertyArgs(methodInvocation.arguments(),field,commonDataType,parentContext,ast);
    }
    else {
      addComplexAddStatement(methodInvocation);
      if(addExtendPropertyArgs())
    extendAddPropertyInfoProcessor.addComplexExtendPropertyArgs(methodInvocation.arguments(),field,commonDataType,parentContext,ast);
    }
  }

  protected boolean addExtendPropertyArgs()
  {return true;}

  protected abstract void addComplexAddStatement(MethodInvocation methodInvocation);

  protected abstract void addSimpleAddStatement(MethodInvocation methodInvocation);

  protected boolean useSimpleAddStatment() {
    if(isI18nPrefixEquals()==false)
      return  false;
    if(isDefaultValueNotNull())
      return false;
    if(field.getIsMultiLanguage())
      return false;
    if( isDefalueNull())
      return false;
    return extendAddPropertyInfoProcessor.useSimpleAddStatement(commonDataType,field,parentContext);
  }

  protected boolean isDefalueNull() {
    return extendAddPropertyInfoProcessor.isDefaultNull(field);
  }

  protected abstract String getMethodName();

  protected void addPropertyNameStatement(MethodInvocation methodInvocation,String propertyName)
  {
    methodInvocation.arguments().add(
        ExpressionUtils.getStringLiteral(ast,propertyName==null?"":propertyName));
  }

  protected void addRequiredArgStatement(MethodInvocation methodInvocation) {
    methodInvocation.arguments().add(ast.newBooleanLiteral(field.getIsRequire()));
  }


  protected void addLengthStatement(MethodInvocation methodInvocation) {
    methodInvocation.arguments().add(ast.newNumberLiteral(String.valueOf(field.getLength())));
  }

  protected void addPrecisionArgStatement(MethodInvocation methodInvocation)
  {
    methodInvocation.arguments().add(ast.newNumberLiteral(String.valueOf(field.getPrecision())));
  }

  protected void addEnableRtrimStatement(MethodInvocation methodInvocation) {
    methodInvocation.arguments().add(ast.newBooleanLiteral(field.isEnableRtrim()));
  }

  protected void addDisplayValueKeyStatement(MethodInvocation methodInvocation) {
    methodInvocation.arguments().add(
        ExpressionUtils.getStringLiteral(ast, field.getI18nResourceInfoPrefix() + "." + "Name"));
  }

  protected void addDefaultValueStatement(MethodInvocation methodInvocation) {
    methodInvocation.arguments().add(InitColumnsGenerator.getDefaultValueExpression(field,ast,parentContext.getApiGenContext()));
  }

  protected void addDefaultNullStatment(MethodInvocation methodInvocation) {
    methodInvocation.arguments().add(ast.newBooleanLiteral(false));
  }

  protected void addIsMultiArgStatement(MethodInvocation methodInvocation) {
    methodInvocation.arguments().add(ast.newBooleanLiteral(field.getIsMultiLanguage()));
  }


  protected boolean isDefaultValueNotNull() {
    return !(field.getDefaultValue() == null || field.getDefaultValue().equals(""));
  }

  protected boolean isI18nPrefixEquals()
  {
    if(field.getI18nResourceInfoPrefix()==null||"".equals(field.getI18nResourceInfoPrefix()))
      return false;
    return field.getI18nResourceInfoPrefix().substring(0,field.getI18nResourceInfoPrefix().lastIndexOf(".")).equals(commonDataType.getI18nResourceInfoPrefix());
  }


}
